import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { ResultData } from 'src/common/utils/result'
import { RequireRole } from 'src/common/decorators/require-role.decorator'
import { CategoryService } from './category.service'
import { CreateCategoryDto, UpdateCategoryDto } from './dto/category.dto'

@ApiTags('分类管理')
@ApiBearerAuth('Authorization')
@Controller('ug/category')
export class CategoryController {
  constructor(private readonly service: CategoryService) {}

  @ApiOperation({ summary: '分类-分页列表' })
  @RequireRole('admin')
  @Get('list')
  async list(@Query() query: any) {
    const data = await this.service.page(query)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '分类-详情' })
  @RequireRole('admin')
  @Get(':id')
  async detail(@Param('id') id: string) {
    const data = await this.service.findOne(+id)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '分类-新增' })
  @RequireRole('admin')
  @Post()
  async create(@Body() dto: CreateCategoryDto) {
    const data = await this.service.create(dto)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '分类-更新' })
  @RequireRole('admin')
  @Put()
  async update(@Body() dto: UpdateCategoryDto) {
    const data = await this.service.update(dto.categoryId, dto)
    return ResultData.ok(data)
  }

  @ApiOperation({ summary: '分类-删除' })
  @RequireRole('admin')
  @Delete(':ids')
  async remove(@Param('ids') ids: string) {
    const arr = ids.split(',').map((i) => +i)
    await this.service.remove(arr)
    return ResultData.ok(true)
  }

  @ApiOperation({ summary: '分类-状态变更' })
  @RequireRole('admin')
  @Put('changeStatus')
  async changeStatus(@Body() body: { categoryId: number; status: number }) {
    const data = await this.service.changeStatus(body.categoryId, body.status)
    return ResultData.ok(data)
  }
}
